#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
    char *dev;
    char *net;
    char *mask;
    int ret;
    char errbuf[PCAP_ERRBUF_SIZE];

    bpf_u_int32 netp;
    bpf_u_int32 maskp;
    struct in_addr addr;

    dev = pcap_lookupdev(errbuf);

    if (dev == NULL) {
        printf("%s\n", errbuf);
        return -1;
    }

    printf("DEV: %s\n", dev);

    ret = pcap_lookupnet(dev, &netp, &maskp, errbuf);

    if (ret < 0) {
        printf("%s\n", errbuf);
        return -1;
    }

    addr.s_addr = netp;
    net = inet_ntoa(addr);

    if (net == NULL) {
        perror("inet_ntoa");
        return -1;
    }

    printf("NET: %s\n", net);

    addr.s_addr = maskp;
    mask = inet_ntoa(addr);

    if (mask == NULL) {
        perror("inet_ntoa");
        return -1;
    }

    printf("MASK: %s\n", mask);

    return 0;
}

